package com.fc.advbox.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.fc.advbox.common.ResponseJson;
import com.fc.advbox.controller.admin.BaseController;
import com.fc.advbox.entity.AdvUserEntity;
import com.fc.advbox.entity.MenuEntity;
import com.fc.advbox.service.IMenuService;
import com.fc.advbox.service.IUserPermService;
import com.fc.advbox.service.IUserRoleService;
import com.fc.advbox.service.IUserService;
import com.fc.advbox.utils.R;
import com.fc.advbox.utils.RRException;
import com.fc.advbox.utils.ShiroUtils;
import com.fc.advbox.utils.annotation.SysLog;
import com.fc.advbox.utils.validator.Assert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;
import java.util.Set;

/**
 * APPLICATION USER API
 *
 * @author Charles Gao
 */
@RestController
@RequestMapping("/api/user")
@Api(value="用户相关的API", tags={"用户接口"})
public class UserController extends BaseController {
	private final IUserService userService;

	private final IUserRoleService roleService;

	private final IUserPermService userPermService;

	private final IMenuService menuService;

    @Autowired
    public UserController(IUserService userService, IUserRoleService roleService, IUserPermService userPermService, IMenuService menuService) {
        this.userService = userService;
        this.roleService = roleService;
        this.userPermService = userPermService;
        this.menuService = menuService;
    }

	@ApiOperation(value = "获取当前登录用户的信息",
			notes = "获取当前登录用户的信息",
			response = R.class,
			httpMethod = "GET",
			consumes = "application/json",
			produces = "application/json"
	)
	@GetMapping("/info")
	public ResponseJson info(){
		ResponseJson responseJson = new ResponseJson();
		Long currentUserId = getUserId();
		if(currentUserId == null)
		{
			throw new RRException("无法获取到用户信息！");
		}
		return responseJson.success(userService.queryDetailInfoById(currentUserId));
	}

	@ApiOperation(value = "修改当前登录用户密码",
			notes = " REST 方式请求 POST" +
					"Request Body: <ul>" +
					"<li><i>必填：password: 原密码</i></li>" +
					"<li><i>必填：newPassword: 新密码</i></li>" +
					"</ul>",
			httpMethod = "POST"
	)
	@SysLog("修改密码")
	@RequestMapping("/password")
	public ResponseJson password(@RequestBody JSONObject params){
		String password = params.getString("password");
		String newPassword = params.getString("newPassword");

		Assert.isBlank(password, "原密码不为能空");
		Assert.isBlank(newPassword, "新密码不为能空");

		//sha256加密
		password = new Sha256Hash(password).toHex();
		//sha256加密
		newPassword = new Sha256Hash(newPassword).toHex();
				
		//更新密码
		userService.updatePassword(getUserId(), password, newPassword);
		//退出
		ShiroUtils.logout();
		
		return ResponseJson.success();
	}

	@ApiOperation(value = "获取指定用户的信息",
			notes = "获取指定用户的信息",
			response = R.class,
			httpMethod = "GET",
			consumes = "application/json",
			produces = "application/json"
	)
	@RequestMapping("/info/{userId}")
	//@RequiresPermissions("api:user:info")
	public ResponseJson info(@PathParam("userId") @PathVariable("userId") Long userId){
		AdvUserEntity user = userService.queryDetailInfoById(userId);
		return ResponseJson.success(user);
	}

	@ApiOperation(value = "获取指定用户的权限",
			notes = "获取指定用户的权限",
			response = R.class,
			httpMethod = "GET",
			consumes = "application/json",
			produces = "application/json"
	)
	@RequestMapping("/perms/{userId}")
	@RequiresPermissions("api:user:perms")
	public ResponseJson perms(@PathParam("userId") @PathVariable("userId") Long userId){
		Set<String> perms = userPermService.queryPermissionSet(userId);
		return ResponseJson.success(perms);
	}

	@ApiOperation(value = "获取指定用户的菜单",
			notes = "获取指定用户的菜单",
			response = ResponseJson.class,
			httpMethod = "GET",
			consumes = "application/json",
			produces = "application/json"
	)
	@GetMapping("/menu/{userId}")
	public ResponseJson menus(@PathParam("userId") @PathVariable("userId") Long userId){
		if(userService.queryObject(userId)== null ){
			return ResponseJson.error("用户不存在");
		}
		List<MenuEntity> menus = menuService.queryByUserIds(userId);
		return ResponseJson.success(menus);
	}
}
